package edu.isistan.matching.adapters;

import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;


import edu.isistan.matching.structures.elements.TypeElement;
import edu.isistan.metamodel.DataType;

public class StringAdapter extends PrimitiveTypeAdapter {

	private static Set<String> fullyCompatible = new HashSet<String>();
	static Hashtable<String, String> codigosHash = new Hashtable<String, String>();

	private static Set<String> loosyCompatible = new HashSet<String>();

	static {
		fullyCompatible.add("base64Binary");
		fullyCompatible.add("hexBinary");
		fullyCompatible.add("string");
		fullyCompatible.add("QName");

		loosyCompatible.add("boolean");
		loosyCompatible.add("byte");
		loosyCompatible.add("unsignedByte");
		loosyCompatible.add("dateTime");
		loosyCompatible.add("decimal");
		loosyCompatible.add("double");
		loosyCompatible.add("float");
		loosyCompatible.add("int");
		loosyCompatible.add("unsignedInt");
		loosyCompatible.add("integer");
		loosyCompatible.add("long");
		loosyCompatible.add("short");
		loosyCompatible.add("unsignedShort");
		loosyCompatible.add("Number");

		codigosHash.put("java.lang.Number",  CODE_MASK + ".toString()");
		
		codigosHash.put("int", " Integer.toString(" + CODE_MASK + ")");
		codigosHash.put("java.lang.Integer", CODE_MASK + ".toString()");

		codigosHash.put("double", " Double.toString(" + CODE_MASK + ")");
		codigosHash.put("java.lang.Double", CODE_MASK + ".toString()");

		codigosHash.put("float", " Float.toString(" + CODE_MASK + ")");
		codigosHash.put("java.lang.Float", CODE_MASK + ".toString()");

		codigosHash.put("long", " Long.toString(" + CODE_MASK + ")");
		codigosHash.put("java.lang.Long", CODE_MASK + ".toString()");
		
		codigosHash.put("short", " Short.toString(" + CODE_MASK + ")");
		codigosHash.put("java.lang.Short", CODE_MASK + ".toString()");

		codigosHash.put("boolean", " Boolean.toString(" + CODE_MASK + ")");
		codigosHash.put("java.lang.Boolean", CODE_MASK + ".toString()"); 
	
		codigosHash.put("byte", " Byte.toString(" + CODE_MASK + ")");
		codigosHash.put("java.lang.Byte", CODE_MASK + ".toString()"); 
		
		codigosHash.put("char", " Character.toString(" + CODE_MASK + ")");
		codigosHash.put("java.lang.Character", CODE_MASK + ".toString()"); 

		codigosHash.put("byte[]", "new String("+CODE_MASK + ")"); 
	
		codigosHash.put("java.math.BigDecimal", CODE_MASK + ".toString()"); 
	
		codigosHash.put("java.util.Calendar", CODE_MASK + ".toString()"); 
	
		//Nuevo 15-03
		codigosHash.put("java.lang.Object", CODE_MASK + ".toString()"); 
		codigosHash.put("Object", CODE_MASK + ".toString()"); 

	}

	@Override
	public Set<String> getFullyCompatibles() {
		return fullyCompatible;
	}

	@Override
	public Set<String> getLoosyCompatibles() {
		return loosyCompatible;
	}

	@Override
	public Object toValue(DataType element) {
		PrimitiveTypeAdapter adapter = AdapterFactory.getAdapter(element);
		return adapter.valueof(getValue());
	}

	@Override
	public Object valueof(Object value) {
		return String.valueOf(value);
	}

	@Override
	public Hashtable<String, String> getLibraryCode() {

		return codigosHash;
	}
	public boolean canBeImplicitConversion(String type1, String type2){
		String wrapperString ="java.lang.String";
		String nativeString ="string";
		if ((wrapperString.equals(type1)&& nativeString.equals(type2) || (wrapperString.equals(type2)&& nativeString.equals(type1))))
		{
			return true;
		}
		return false;
	}
}
